package com.example.admin.goodnews.fragment.ZaiXianYinYue;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.activity.SecondaryPageActivity;
import com.example.admin.goodnews.adapter.PaiHangBangGeDanAdapter;
import com.example.admin.goodnews.adapter.SouSuoJieGuoAdapter;
import com.example.admin.goodnews.entity.PaiHangBangGeDanDataEntity;
import com.example.admin.goodnews.entity.SouSuoJieGuoDataEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemClickListener;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

/**
 * 排行榜歌单页面
 * Created by admin on 2016/10/18.
 */

public class PaiHangBangSouSuoFragment extends Fragment implements RecyclerViewItemClickListener, RecyclerViewItemLongClickListener {
    private RecyclerView recyclerview_paihangbangsousuo;
    private TextInputLayout til_paihangbangsousuo;
    private EditText et_paihangbangsousuo;
    private Button btn_paihangbangsousuo;
    private LinearLayoutManager linearLayoutManager;// 布局管理器
    private SouSuoJieGuoDataEntity souSuoJieGuoDataEntity;// 数据实体类
    private SouSuoJieGuoAdapter souSuoJieGuoAdapter;// 数据适配器
    private String time = GetCurrentTimeUtil.getCurrentTime();// 获取当前系统时间
    private String name = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_paihangbangsousuo, null);
        recyclerview_paihangbangsousuo = (RecyclerView) view.findViewById(R.id.recyclerview_paihangbangsousuo);
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerview_paihangbangsousuo.setLayoutManager(linearLayoutManager);// 设置布局管理器
        btn_paihangbangsousuo = (Button) view.findViewById(R.id.btn_paihangbangsousuo);
        til_paihangbangsousuo = (TextInputLayout) view.findViewById(R.id.til_paihangbangsousuo);
        et_paihangbangsousuo = til_paihangbangsousuo.getEditText();
        et_paihangbangsousuo.setHint("请输入歌曲名称并搜索");
        TextChange textChange = new TextChange();
        et_paihangbangsousuo.addTextChangedListener(textChange);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 搜索按钮点击事件
        btn_paihangbangsousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static PaiHangBangSouSuoFragment newInstance() {
        PaiHangBangSouSuoFragment paiHangBangGeDanFragment = new PaiHangBangSouSuoFragment();
        return paiHangBangGeDanFragment;
    }

    private void getData() {
        String param = NetWorkDataUrl.PAIHANGBANGSOUSUO_PARAM1 + name + NetWorkDataUrl.PAIHANGBANGSOUSUO_PARAM2 + time + NetWorkDataUrl.PAIHANGBANGSOUSUO_PARAM3;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.PAIHANGBANGSOUSUO_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    souSuoJieGuoDataEntity = gson.fromJson(result, SouSuoJieGuoDataEntity.class);
                    if (souSuoJieGuoDataEntity.getShowapi_res_body().getPagebean().getContentlist().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    souSuoJieGuoAdapter = new SouSuoJieGuoAdapter(getActivity(), souSuoJieGuoDataEntity);
                    recyclerview_paihangbangsousuo.setAdapter(souSuoJieGuoAdapter);
                    souSuoJieGuoAdapter.setRecyclerViewItemClickListener(PaiHangBangSouSuoFragment.this);
                    souSuoJieGuoAdapter.setRecyclerViewItemLongClickListener(PaiHangBangSouSuoFragment.this);
                    break;
            }
        }
    };

    @Override
    public void RecyclerViewItemClickListener(int position) {
        String title = souSuoJieGuoAdapter.getTitle(position);
        String link = souSuoJieGuoAdapter.getLink(position);
        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("link", link);
        startActivity(intent);
    }

    @Override
    public void RecyclerViewItemLongClickListener(int position) {
        String title = souSuoJieGuoAdapter.getTitle(position);
        String link = souSuoJieGuoAdapter.getLink(position);
        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("link", link);
        startActivity(intent);
    }

    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            name = s.toString();
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() > 0 && !"".equals(s)) {
                btn_paihangbangsousuo.setEnabled(true);
            } else {
                btn_paihangbangsousuo.setEnabled(false);
            }
        }
    }

}